package com.sisSaude.collections;

import java.util.Vector;

import com.sisSaude.daos.ItemDeControleDeFrequenciaCardiacaDAO;
import com.sisSaude.entityBeans.ItemDeControleDeFrequenciaCardiaca;
import com.sisSaude.utils.DataProvider;

public class ItensDeControleDeFrequenciaCardiacaCollection extends GenericCollection<ItemDeControleDeFrequenciaCardiaca>  {

	public ItensDeControleDeFrequenciaCardiacaCollection(int idControle, String orderBy)	{
		String sql = ItemDeControleDeFrequenciaCardiacaDAO.SELECT_BASICA + "" +
		        " WHERE ItemDeControleDeFrequenciaCardiaca.idControle = ? " +
				" ORDER BY " + orderBy;
		Vector<Object> params = new Vector<Object>();
		params.add(idControle);
		this.load(sql, params);
	}
	public ItemDeControleDeFrequenciaCardiaca findByDuracao(int duracao) {
		for (ItemDeControleDeFrequenciaCardiaca item : getItens()) {
			if (item.getDuracao() == duracao)
				return item;
		}
		return null;
	}
	public ItemDeControleDeFrequenciaCardiaca findByBpmMaximo() {
		ItemDeControleDeFrequenciaCardiaca itemBpmMaximo = null;
		for (ItemDeControleDeFrequenciaCardiaca item : getItens()) {
			if (itemBpmMaximo == null || item.getBpm() > itemBpmMaximo.getBpm())
				itemBpmMaximo = item;
		}
		return itemBpmMaximo;
	}
	
	private void load(String sql, Vector<Object> params) {
		ItemDeControleDeFrequenciaCardiaca item = null;
		ItemDeControleDeFrequenciaCardiacaDAO dao = new ItemDeControleDeFrequenciaCardiacaDAO();
		DataProvider dataProvider = new DataProvider();
		dataProvider.select(sql, params);
		try {
			while (dataProvider.next()) {
				item = new ItemDeControleDeFrequenciaCardiaca();
				dao.loadAttribs(item, dataProvider.getResultSet());
				this.getItens().add(item);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dataProvider.close();
		}
	}
}
